package com.snailxr.base.task.dao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Repository;

import com.mongodb.WriteResult;
import com.snailxr.base.task.dao.DataDictionaryDao;
import com.snailxr.base.task.domain.APIObj;
import com.snailxr.base.task.domain.DataDictionary;
import com.snailxr.base.task.domain.ScheduleJob;
@Repository("dataDictionaryDaoImpl")
public class DataDictionaryDaoImpl implements DataDictionaryDao {
	public final Logger log = Logger.getLogger(this.getClass());
	@Resource(name="mongoTemplate")
	private MongoTemplate mongoTemplate;
	private String collectionName;//集合名
	
	@Override
	public List<DataDictionary> selectByType(String dicType) {
		List<DataDictionary> ddics = mongoTemplate.find(new Query(Criteria.where("dicType").is("1")), DataDictionary.class);
		return ddics;
	}

	@Override
	public List<DataDictionary> selectAll() {
		List<DataDictionary> dataDictionary = mongoTemplate.findAll(DataDictionary.class);
		return dataDictionary;
	}

	@Override
	public void insert(DataDictionary ddic) {
		mongoTemplate.insert(ddic,collectionName);
	}

	@Override
	public DataDictionary selectByName(String dicName) {
		// TODO 自动生成的方法存根
		return null;
	}

	public MongoTemplate getMongoTemplate() {
		return mongoTemplate;
	}

	public void setMongoTemplate(MongoTemplate mongoTemplate) {
		this.mongoTemplate = mongoTemplate;
	}

	public String getCollectionName() {
		return collectionName;
	}

	public void setCollectionName(String collectionName) {
		this.collectionName = collectionName;
	}

	@Override
	public Boolean update(DataDictionary dicObj) {
		Query query = new Query(Criteria.where("dicId").is(dicObj.getDicId()));
		Update update = new Update();
		update.set("dicName",dicObj.getDicName());
		update.set("dicType",dicObj.getDicType());
		update.set("channelId",dicObj.getChannelId());
		update.set("orgcode",dicObj.getOrgCode());
		update.set("orgName",dicObj.getOrgName());
		update.set("regiod", dicObj.getRegion());
		update.set("regionCode",dicObj.getRegionCode());
		WriteResult writeResult = mongoTemplate.updateFirst(query, update, DataDictionary.class);
		log.debug(writeResult.toString());
		return true;
	}

	@Override
	public void delete(int dicId) {
		mongoTemplate.remove(new Query(Criteria.where("dicId").is(dicId)), DataDictionary.class);
	}

	@Override
	public DataDictionary selectByDicId(int dicId) {
		return mongoTemplate.findOne(new Query(Criteria.where("dicId").is(dicId)), DataDictionary.class);
	}

}
